<?php

namespace Usuarios\Model;

//use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Predicate\Expression;

require_once('nusoap/nusoap.php');
use nusoap_client as SoapClient;

class FuncionarioTable {

/*   protected $tableGateway;

   public function __construct(TableGateway $tableGateway) {
      $this->tableGateway = $tableGateway;
   }*/

   private $client;

   public function __construct() {
      $this->client = new SoapClient('http://soap.asistencia.uca.edu.py/usuarios_soap.php');
   }

/*   public function fetchAll() {
      $resultSet = $this->tableGateway->select();
      return $resultSet;
   }*/

   //SOAP Client Version
   public function fetchAll() {

      $result = $this->client->call('funcionariotable_fetchAll',array());
      //var_dump($result);

      //convertimos la respuesta de array a objetos
      $rowset = json_decode(json_encode($result), FALSE);

      return $rowset;
   }

/*   public function deleteMissingIDs() {
      $select  = new \Zend\Db\Sql\Select;
      //SELECT F.id FROM funcionarios F LEFT JOIN usuarios U ON F.id = U.id_funcionario WHERE U.id_funcionario is null;
      $select->from('funcionarios');
      $select->columns(array('id'));
      $select->join('usuarios',"usuarios.id_funcionario = funcionarios.id",array('id_fun' => 'id_funcionario'),'left');
      $select->where('usuarios.id_funcionario is null');
      //echo $select->getSqlString();

      $rowset = $this->tableGateway->selectWith($select);
      foreach($rowset as $row) {
         $this->deleteFuncionario($row->id);
      }
   }*/

 //SOAP Client Version
   public function deleteMissingIDs() {

      $result = $this->client->call('funcionariotable_getMissingIDs',array());

      //convertimos la respuesta de array a objetos
      $rowset = json_decode(json_encode($result), FALSE);
      foreach($rowset as $row) {
         $this->deleteFuncionario($row->id);
      }
   }

/*   public function getFuncionario($id) {
      $id = (int) $id;
      $rowset = $this->tableGateway->select(array('id' => $id));
      $row = $rowset->current();
      if (!$row) {
         throw new \Exception("Could not find row $id");
      }
      return $row;
   }*/

   //SOAP Client Version
   public function getFuncionario($id) {
      $id = (int) $id;

      $result = $this->client->call('getFuncionario',array('id' => $id));

      if (empty($result)) {
         throw new \Exception("Could not find row $id");
      }
      $rowset = json_decode(json_encode($result), FALSE);
      return $rowset[0];
   }

/*   public function getlastFuncionario() {
      $select = $this->tableGateway->getSql()->select();
      $select->order('id DESC')
             ->limit(1);
      $rowset = $this->tableGateway->selectWith($select);
      $row = $rowset->current();
      if (!$row) {
         throw new \Exception("Could not find last Profesor");
      }
      return $row;
   }*/

  //SOAP Client Version
   public function getlastFuncionario() {

      $result = $this->client->call('getlastFuncionario', array());

      if (empty($result)) {
         throw new \Exception("Could not find last Funcionario");
      }
      $rowset = json_decode(json_encode($result), FALSE);
      return $rowset[0];
   }

/*   public function saveFuncionario(Funcionario $funcionario) {
      $data = array(
         'fecha_ingreso' => $funcionario->fecha_ingreso,
      );

      $id = (int)$funcionario->id;
      if ($id == 0) {
         if (is_null($data['fecha_ingreso']))
            $data['fecha_ingreso'] = new Expression('default');
         $this->tableGateway->insert($data);
      } else {
         if ($this->getFuncionario($id)) {
            $this->tableGateway->update($data, array('id' => $id));
         } else {
            throw new \Exception('id does not exist');
         }
      }
   }*/

   //SOAP Client Version
   public function saveFuncionario(Funcionario $funcionario) {

      $id = (int)$funcionario->id;
      if ($id == 0) {
         $this->client->call('insertFuncionario', array());
      } else {
         if ($this->getFuncionario($id)) {
            $this->client->call('updateFuncionario', array('fecha_ingreso' => $profesor->fecha_ingreso));
         } else {
            throw new \Exception('id does not exist');
         }
      }
   }

/*   public function deleteFuncionario($id) {
      $this->tableGateway->delete(array('id' => $id));
   }*/

   //SOAP Client version
   public function deleteFuncionario($id) {
      $this->client->call('deleteFuncionario', array('id' => $id));
   }

}
